package com.hrms.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hrms.pojo.Employee;
import com.hrms.pojo.Pay;
import com.hrms.service.EmployeeService;
import com.hrms.service.PayService;

@Controller
@RequestMapping("/pay")
public class PayController {
	
	@Resource
	private PayService payService;
	
	@Resource
	private EmployeeService employeeService;
	
	//查询出所有薪资记录
	@RequestMapping(value="/payList")
	public String getPayList(Model model,HttpServletRequest request){
		List<Pay> payList=payService.selectAll();
		request.getSession().setAttribute("empAll",employeeService.getEmpAll());
		model.addAttribute("payList",payList);
		return "pay/pay_list";
	}
	
	//根据员工id查询薪资记录
	@RequestMapping(value="/payListForEm")
	public String selectByEmNumber(Model model,HttpServletRequest request){
		String emId=request.getParameter("emId");
		if(emId==null){
			return "forward:/error/error";
		}
		int newEmId=Integer.parseInt(emId);
		List<Pay> payList=payService.selectByEmNumber(newEmId);
		model.addAttribute("payList",payList);
		return "pay/pay_list";
	}
	
	//根据年月查询薪资记录
	@RequestMapping(value="/payListForDate")
	public String selectByDate(Model model,HttpServletRequest request){
		String year=request.getParameter("year");
		String month=request.getParameter("month");
		List<Pay>payList=payService.selectByDate(year, month);
		model.addAttribute("payList",payList);
		return "pay/pay_list"; 
	}
	
	//根据id删除薪资记录
	@RequestMapping(value="/{payId}/removePayById")
	public String deleteById(@PathVariable("payId") Integer payId,Model model,HttpServletRequest request){
		payService.deleteById(payId);
		request.getSession().setAttribute("allPay", payService.selectAll());
		model.addAttribute("payList",payService.selectAll());
		return "pay/pay_list"; 
	}
	
	//添加薪资信息
	@RequestMapping(value="/addPay")
	public String addPay(Model model){
		List<Employee> empList=employeeService.getEmpAll();
		model.addAttribute("empList",empList);
		return "pay/add_pay";
	}
	
	//根据员工id查找员工姓名
	@RequestMapping(value="/getEmName")
	public void getEmNameById(HttpServletRequest request,HttpServletResponse response){
		String id=request.getParameter("emid");
		response.setContentType("type=text/html;charset=UTF-8");
		try {
			Employee employee=employeeService.getEmpById(Integer.parseInt(id));
			response.getWriter().write(employee.getEmName());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//添加薪资信息
	@RequestMapping(value="addaPay")
	public String addPay(HttpServletRequest request,Model model){
		Pay pay=new Pay();
		String emId=request.getParameter("empId");
		pay.setPayEmNumber(emId);
		String emName=request.getParameter("empName");
		pay.setPayEmName(emName);
		String payGrantTime=request.getParameter("date");
		pay.setPayGrantTime(payGrantTime);
		String payBase=request.getParameter("payBase");
		if(payBase!=null){
			pay.setPayBase(Integer.parseInt(payBase));
		}
		String payOverTime=request.getParameter("payOverTime");
		System.out.println(payOverTime);
		if(payOverTime!=null&&payOverTime.trim()!=""){
			pay.setPayOvertime(Integer.parseInt(payOverTime));
		}
		String payAge=request.getParameter("payAge");
		if(payAge!=null&&payAge.trim()!=""){
			pay.setPayAge(Integer.parseInt(payAge));
		}
		String payCheck=request.getParameter("payCheck");
		if(payCheck!=null&&payCheck.trim()!=""){
			pay.setPayCheck(Integer.parseInt(payCheck));
		}
		String payAbsent=request.getParameter("payAbsent");
		if(payAbsent!=null&&payAbsent.trim()!=""){
			pay.setPayAbsent(Integer.parseInt(payAbsent));
		}
		String paySafety=request.getParameter("paySafety");
		if(paySafety!=null&&paySafety.trim()!=""){
			pay.setPaySafety(Integer.parseInt(paySafety));
		}
		payService.insertPay(pay);
		model.addAttribute("payList",payService.selectAll());
		return "pay/pay_list";
	}
	
	//分页查找薪资信息
	@RequestMapping("limit")
	public String selectPayLimit(HttpServletRequest request,Model model){
		int pageSize = 5;// 每页显示5条
		List<Pay> allPay = payService.selectAll();
		request.getSession().setAttribute("allPay", allPay);
		int total = allPay.size();
		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
		int showPage = 1;
		try {
			showPage = Integer.parseInt(request.getParameter("pageIndex"));
			if (showPage <= 1)
				showPage = 1;
		} catch (NumberFormatException e) {
			showPage = 1;
		}
		if (showPage > totalPage)
			showPage = totalPage;
		int offset = (showPage - 1) * pageSize;
		int limit = showPage * pageSize;
		request.getSession().setAttribute("pageIndex", showPage);
		List<Pay> payList = payService.selectByPage(offset, limit);
		model.addAttribute("payList", payList);
		return "pay/limit_pay";
	}
}
